layui.config({
	base: '/static/admin/js/module/'
}).extend({
	dialog: 'dialog',
});

layui.use(['form', 'jquery', 'laydate', 'layer', 'laypage', 'dialog',   'element'], function() {
	var form = layui.form(),
		layer = layui.layer,
		$ = layui.jquery,
		dialog = layui.dialog;
	//获取当前iframe的name值
	var iframeObj = $(window.frameElement).attr('name');
	// 全选 - 列表页
	form.on('checkbox(allChoose)', function(data) {
		var child = $('input[lay-filter="oneChoose"]');
		child.each(function(index, item) {
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});
	// 单选 - 列表页
    form.on('checkbox(oneChoose)', function (data) {
        var item = $('input[lay-filter="oneChoose"]');
		var c_all = $('input[lay-filter="allChoose"]');
		var tmp_ck = false;
        for (var i = 0; i < item.length; i++) {
            if (item[i].checked == false) {
                tmp_ck = false;
                break;
            }else{
				tmp_ck = true;
			}
        }
        c_all.prop("checked", tmp_ck);
        form.render('checkbox');
    });
	//渲染表单
	form.render();	
	//顶部添加
	$('.addBtn').click(function() {
		var url=$(this).attr('data-url');
		var partner = $(this).attr('partner');
		//console.log('partner:'+partner);
		if (partner != null && partner != 'undefined'){
			if (url.indexOf('?')>0){ url += '&'; }else{ url += '?'; }
			url += partner+'='+$('#'+partner).val();
		}
		//将iframeObj传递给父级窗口,执行操作完成刷新
		if($(this).attr('target') === "_blank"){
			parent.page("添加", url, iframeObj, w = "700px", h = "620px");
		}else{
			window.location.href=url;
		}
		return false;

	}).mouseenter(function() {
		dialog.tips('添加', '.addBtn');
	})
	//顶部排序
	$('.listOrderBtn').click(function() {
		var url=$(this).attr('data-url');
		dialog.confirm({
			message:'您确定要进行排序吗？',
			success:function(){
				layer.msg('确定了')
			},
			cancel:function(){
				layer.msg('取消了')
			}
		})
		return false;

	}).mouseenter(function() {

		dialog.tips('批量排序', '.listOrderBtn');

	})	
	//顶部批量删除
	$('.delBtn').click(function() {
		var surl=$(this).attr('data-url');
		dialog.confirm({
			message:'您确定要删除选中项',
			success:function(){
				var iids = "";
				$('input[lay-filter="oneChoose"]').each(function(){
					if ($(this).prop("checked")){
						iids += $(this).attr('data-id') + ',';
					}
				});
				if (iids.length<=0){
					layer.msg('请先选择要删除的项！');
					return;
				}
				$.ajax({
					url: surl,
					type: "POST",
					data: {ids: iids},
					cache: false,
					dataType: 'json',
					success: function(response){
						layer.msg(response.msg);
						if (response.code == 200){
							refresh();
						}
					},
					error:function(response) {
						layer.msg("ajax error: " + response);
					},
				});
			},
			cancel:function(){
				//layer.msg('取消了')
			}
		})
		return false;

	}).mouseenter(function() {

		dialog.tips('批量删除', '.delBtn');

	})	
	//列表添加
	$('#table-list').on('click', '.add-btn', function() {
		var url=$(this).attr('data-url');
		var partner = $(this).attr('partner');
		//console.log('partner:'+partner);
		if (partner != null && partner != 'undefined'){
			url += '?'+partner+'='+$('#'+partner).val();
		}
		//将iframeObj传递给父级窗口,执行操作完成刷新
		if($(this).attr('target') === "_blank"){
			parent.page("添加", url, iframeObj, w = "700px", h = "620px");
		}else{
			window.location.href=url;
		}
		return false;
	})
	//列表删除
	$('#table-list').on('click', '.del-btn', function() {
		var surl=$(this).attr('data-url');
		var iid = $(this).attr('data-id');
		dialog.confirm({
			message:'您确定要进行删除吗？',
			success:function(){
				$.ajax({
					url: surl,
					type: "POST",
					data: {id: iid},
					cache: false,
					dataType: 'json',
					success: function(response){
						layer.msg(response.msg);
						if (response.code == 200){
							refresh();
						}
					},
					error:function(response) {
						layer.msg("ajax error: " + response);
					},
				});
			},
			cancel:function(){
				//layer.msg('取消了')
			}
		})
		return false;
	})
	//列表跳转
	$('#table-list,.tool-btn').on('click', '.go-btn', function() {
		var url=$(this).attr('data-url');
		var id = $(this).attr('data-id');
		if (url.indexOf('?')>0){
			url += "&";
		}else{ url += '?';}
		url += "id="+id;
		window.location.href=url;
		return false;
	})
	//编辑栏目
	$('#table-list').on('click', '.edit-btn', function() {
		var That = $(this);
		//var id = That.attr('data-id');
		var url=That.attr('data-url');
		//将iframeObj传递给父级窗口
		//parent.page("菜单编辑", url + "?id=" + id, iframeObj, w = "700px", h = "620px");
		var partner = $(this).attr('partner');
		//console.log('partner:'+partner);
		if (partner != null && partner != 'undefined'){
			url += '?'+partner+'='+$('#'+partner).val();
		}
		//将iframeObj传递给父级窗口,执行操作完成刷新
		if($(this).attr('target') === "_blank"){
			parent.page("编辑", url, iframeObj, w = "700px", h = "620px");
		}else{
			window.location.href=url;
		}
		return false;
	})
});

/**
 * 控制iframe窗口的刷新操作
 */
var iframeObjName;

//父级弹出页面
function page(title, url, obj, w, h) {
	if(title == null || title == '') {
		title = false;
	};
	if(url == null || url == '') {
		url = "404.html";
	};
	if(w == null || w == '') {
		w = '700px';
	};
	if(h == null || h == '') {
		h = '350px';
	};
	iframeObjName = obj;
	//如果手机端，全屏显示
	if(window.innerWidth <= 768) {
		var index = layer.open({
			type: 2,
			title: title,
			area: [320, h],
			fixed: false, //不固定
			content: url
		});
		layer.full(index);
	} else {
		var index = layer.open({
			type: 2,
			title: title,
			area: [w, h],
			fixed: false, //不固定
			content: url
		});
	}
}

/**
 * 刷新子页,关闭弹窗
 */
function refresh() {
	//根据传递的name值，获取子iframe窗口，执行刷新
	if(window.frames[iframeObjName]) {
		window.frames[iframeObjName].location.reload();

	} else {
		window.location.reload();
	}

	layer.closeAll();
}

function reloadiFrame(){
	//根据传递的name值，获取子iframe窗口，执行刷新
	if(window.frames[iframeObjName]) {
		window.frames[iframeObjName].reloadSearch();
	
	} else {
		window.location.reload();
	}
	
	layer.closeAll();
}